import 'package:flutter/material.dart';
import 'package:flutter_enjoy_plus/pages/house/location.dart';
import 'package:flutter_enjoy_plus/pages/login/index.dart';
import 'package:flutter_enjoy_plus/pages/notice/index.dart';
import 'package:flutter_enjoy_plus/pages/profile/index.dart';
import 'package:flutter_enjoy_plus/utils/TokenManager.dart';

import 'pages/404.dart';
import 'pages/house/house_list.dart';
import 'pages/tabs_page.dart';
// 调试错误问题

void main() {
  runApp(MaterialApp(
    routes: {
      '/': (context) => const TabsPage(),
      '/notice_detail': (context) => const NoticeDetail(),
      '/login': (context) => const LoginPage(),
    },
    initialRoute: '/',
    // 路由拦截 -- pushNamed跳转时 && 访问的页面路径并不是上方的routes中的路径时，会执行这个方法
    onGenerateRoute: (settings) {
      // settings 路由信息 路由名称name 路由参数arguments
      final token = tokenManager.getToken();
      if (token == '') {
        return MaterialPageRoute(builder: (context) => const LoginPage());
      } else {
        if (settings.name == '/profile') {
          return MaterialPageRoute(
            builder: (context) => ProfilePage(
                userInfo: settings.arguments as Map<String, dynamic>),
          );
        } else if (settings.name == '/house') {
          return MaterialPageRoute(
            builder: (context) => const HouseList(),
          );
        } else if (settings.name == '/add_house') {
          return MaterialPageRoute(
            builder: (context) => const LocationList(),
          );
        }
      }
      return MaterialPageRoute(builder: (context) => const NotFound());
    },
  ));
}
